<template>
  <div class='timer_play'>
    <div class='time_warp'>
      <div class='time_item' :key="index" v-for="(item, index) in trackBackData">
        <ga-tooltip  placement='top' :content="dayjs(item.time).format(dateTime)">
          <div class='time_item_rect' @click="() => timeClick(item)" :style="getTimeRectColor(item)"></div>
        </ga-tooltip>
        <div class='time_item_label'>{{ getItemText(item, index) }}</div>
      </div>

    </div>

    <icon-pause-circle class='time_play_icon' @click="() => playerState.isPlay = false" v-show="playerState.isPlay" />

    <icon-play-circle class='time_play_icon' @click="() => playerState.isPlay = true" v-show="!playerState.isPlay" />

    <icon-refresh  class='time_play_icon' @click="reloadTime" />
  </div>
</template>

<script setup>
import { watch, reactive } from 'vue'
import { findIndex } from 'lodash'
import dayjs from 'dayjs';
const props = defineProps({
  trackBackData: {
    type: Array,
    default: () => []
  }
})
const dateTime = 'YYYY-MM-DD HH:mm:ss'
const emits = defineEmits(['on-time-change'])
const playerState = reactive({
  isPlay: false,
  currentTime: 0,
  showMs: false, // 时间间隔大于1天-展示MM-DD, 小于1天-展示HH:mm
})
let timer;
let timeIndex = 0;
watch(() => props.trackBackData, (newV) => {
  if (newV.length) {
    playerState.currentTime = newV[0].time;
    emits('on-time-change', newV[0])
    const timeDiff = newV[newV.length - 1].time - newV[0].time;
    playerState.showMs = timeDiff <= 24 * 3600 * 1000
  }
})

watch(() => playerState.isPlay, (newV) => {
  if (newV) {
    let tempCurrentTime = playerState.currentTime;
    // 若为已经播放到最后一个时间点，再次点击播放，需要重头播放
    if (playerState.currentTim === props.trackBackData[props.trackBackData.length - 1].time) {
      tempCurrentTime = props.trackBackData[0].time;
      // setCurrentTime(tempCurrentTime);
      playerState.currentTime = tempCurrentTime
      emits('on-time-change', props.trackBackData[0])
    }
    timeIndex = findIndex(props.trackBackData, { time: tempCurrentTime });
    clearInterval(timer);
    timer = setInterval(() => {
      if (timeIndex < props.trackBackData.length - 1) {
        const trackBackItem = props.trackBackData[++timeIndex];
        playerState.currentTime = trackBackItem.time;
        emits('on-time-change', trackBackItem)
      } else {
        clearInterval(timer);
        timeIndex = 0;
        playerState.isPlay = false;
      }
    }, 1500);
  } else {
    clearInterval(timer);
    playerState.isPlay = false;
  }
})
const getTimeRectColor = (trackBackItem) => {
  const { time, isError } = trackBackItem;
  if (time > playerState.currentTime) {
    return isError ? { background: '#F7313B' } : {}
  } else {
    return isError ? { background: 'linear-gradient(90deg, #2736c7 0%, #2736c7 50%, #F7313B 50%, #F7313B)' } : { background: '#2736c7' }
  }
}
const getItemText = (item, idx) => {
  if (idx % 6 === 0 || idx === 29) {
    return dayjs(item.time).format(playerState.showMS ? 'HH:mm' : 'MM-DD');
  } else {
    return ''
  }
}
const timeClick = (trackBackItem) => {
  playerState.currentTime = trackBackItem.time;
  playerState.isPlay = false
  emits('on-time-change', trackBackItem)
}

const reloadTime = () => {
  if (props.trackBackData.length) {
    playerState.currentTime = props.trackBackData[0].time
    playerState.isPlay = false
    // onTimeChange(trackBackData[0]);
    emits('on-time-change', props.trackBackData[0])
  }
};
</script>

<style scoped lang="scss">
// 回溯计时器
.timer_play {
  height: 70px;
  width: 670px;
  background-color: #F2F3FC;
  border-radius: 4px;
  padding: 0 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .time_warp {
    display: flex;
    justify-content: space-between;
    flex: 1;
    padding: 0 12px;

    .time_item {
      position: relative;

      &_rect {
        width: 6px;
        height: 12px;
        border-radius: 4px;
        cursor: pointer;
        background: #DAE0F7;
      }

      &_label {
        position: absolute;
        font-size: 12px;
        width: max-content;
        transform: translateX(-50%);
      }
    }
  }

  .time_play_icon {
    font-size: 16px;
    margin-left: 8px;
    cursor: pointer;

    &:hover {
      color: var(--span-a-color);
    }
  }
}
</style>
